import numpy as np
import cv2
ix, iy = -1, -1  # 矩形固定左上角
drawing = False
# 查询鼠标事件
events = [i for i in dir(cv2) if 'EVENT' in i]
print(events)


# 创建滑动条的回调函数
def nothing(x):
    pass


# 鼠标回调函数
def Draw(event, x, y, flags, param):
    global ix, iy
    if event == cv2.EVENT_LBUTTONDOWN:
        drawing = True
        ix, iy = x, y
    # 获取当前滑块位置
    r = cv2.getTrackbarPos('R', 'image')
    g = cv2.getTrackbarPos('G', 'image')
    b = cv2.getTrackbarPos('B', 'image')
    color = [b, g, r]  # 将滑块的位置数据传递给color致画图时修改颜色

    if k == ord('1'):
        cv2.rectangle(img, (ix, iy), (x, y), color, -1)#填充
    elif k == ord('2'):
        cv2.rectangle(img, (ix, iy), (x, y), color, 1)#不填冲
    elif event == cv2.EVENT_MOUSEMOVE and flags == cv2.EVENT_FLAG_RBUTTON:
        cv2.circle(img, (x, y), 3, color, -1)  # 单击右键画圆


# 创建一块黑色背景图
img = np.zeros((400, 600, 3), np.uint8)
cv2.namedWindow('image')
cv2.setMouseCallback('image', Draw)  # 鼠标事件反馈处理

# 创建滑动条
cv2.createTrackbar('R', 'image', 0, 255, nothing)
cv2.createTrackbar('G', 'image', 0, 255, nothing)
cv2.createTrackbar('B', 'image', 0, 255, nothing)
img[:] = 255  # 将黑色面板化为白色面板

while 1:
    cv2.imshow('image', img)  # 显示输出图像
    k = cv2.waitKey(1) & 0XFF
    if k == 27:  # 当输入Esc时推出循环
        break

